import { login } from '@/http/user'

export let user = {
    namespaced: true,
    state: {
        token: localStorage.getItem('houtaiToken'),
        get username() {
            return localStorage.getItem('username')
        },
        set username(v){
            if(v){
                localStorage.setItem('username',v)
            }else{
                localStorage.removeItem('username')
            }
        }
    },
    actions: {
        async loginFn({ commit }, obj) {
            let res = await login(obj)
            console.log(res);
            if (res.meta.status == 200) {

                commit('TOKEN', res.data.token)
                commit('USERNAME', res.data.username)
                return Promise.resolve()
            } else {
                return Promise.reject()
            }
        }
    },
    mutations: {
        TOKEN(state, token) {
            state.token = token
            localStorage.setItem('houtaiToken', token)
        },
        USERNAME(state, username) {
            state.username = username
        },
        DEL_token(state){
            localStorage.removeItem('houtaiToken')
        },
        DEL_username(state){
            state.username=''
        }
    }
}